package interviewClassic150;

import org.junit.Test;

public class Convert {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;

        Character[][] arr = new Character[numRows][1000];
        char[] chars = s.toCharArray();
        int lastColumn = 0;
        int curRow = 0;
        boolean positiveDirection = true;
        for (int i = 0; i < chars.length; i++) {
            char cur = chars[i];
            arr[curRow][lastColumn] = cur;
            if (!positiveDirection && curRow == 0) positiveDirection = true;
            else if (positiveDirection && curRow == numRows - 1) positiveDirection = false;
            //确定下一个要插入的位置
            curRow = positiveDirection ? curRow + 1 : curRow - 1;
            lastColumn = positiveDirection ? lastColumn : lastColumn + 1;
        }
        //创建StringBuilder来拼接字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j <= lastColumn; j++) {
                if (arr[i][j] != null) {
                    sb.append(arr[i][j]);
                }
            }
        }

        return sb.toString();
    }

    @Test
    public void test() {
        String s = "PAYPALISHIRING";
        int numRows = 3;
        String convert = convert(s, numRows);
        System.out.println(convert);
    }
}
